#!/usr/bin/env python
# coding=utf-8
from google.appengine.ext import db
import hashlib
from google.appengine.api.datastore_types import GeoPt
import datetime
from geo import geohash
from google.appengine.api import memcache
from config import LOCATION_PRECISION
from location import memb_loc_info, memb_latest_modify_time, members_in_memery
import time
from google.appengine.ext.db import NotSavedError
import logging

class Member(db.Model):
    #以下为注册时的信息
    username = db.StringProperty(required=True,indexed=True)
    password = db.StringProperty(required=True)
    email = db.StringProperty(required=True)
    register_time = db.DateTimeProperty(auto_now_add=True)
    ip = db.StringProperty(default="")
    auth = db.StringProperty(default="")
    #以下为个人资料
    birthday = db.DateProperty(default=datetime.date(1900,01,01))
    gender = db.StringProperty(choices=set(["1","0","-1"]),default="-1")#1 male,0 female,-1 unkown
    nickname = db.StringProperty(default="")
    mark = db.StringProperty(default="")#个性签名
    city = db.StringProperty(default="")
    adress = db.StringProperty(default="")
    latest_login_time = db.DateTimeProperty(auto_now_add=True)
    avatar_url = db.StringProperty(default="")
    
    def verify_user(self,username,password):
        u = username.strip()
        p = password.strip()
        if (len(u) > 0 and len(p) > 0):
            p_sha1 = hashlib.sha1(p).hexdigest()
            one = False
            if members_in_memery.has_key(username):
                one = members_in_memery[username]
                if one.password == p_sha1:
                    return one
            q = db.GqlQuery("SELECT * FROM Member WHERE username = :1 AND password = :2", u, p_sha1)
            if (q.count() == 1):
                return q[0]
        return False
    verify_user = staticmethod(verify_user)
    
class PassbyItem(db.Model):
    item_id = db.IntegerProperty(indexed=True)
    member = db.ReferenceProperty(Member)
    created_time = db.DateTimeProperty(auto_now_add=True)
    content = db.TextProperty()
    comments = db.IntegerProperty(default=0)
    location = db.GeoPtProperty()
    
class Comment(db.Model):
    comment_id = db.IntegerProperty(indexed=True)
    member = db.ReferenceProperty(Member)
    item = db.ReferenceProperty(PassbyItem)
    created_time = db.DateTimeProperty(auto_now_add=True)
    content = db.TextProperty()
    location = db.GeoPtProperty()
    hash_location = db.ComputedProperty(lambda self:geohash.encode(self.location.lat, self.location.lon, 12))
    
class Avatar(db.Model):
    id = db.IntegerProperty(indexed=True)
    name = db.StringProperty(required=False, indexed=True)
    content = db.BlobProperty()
    
class Location(db.Model):
    member = db.ReferenceProperty(Member, required=True)
    location = db.GeoPtProperty(default=GeoPt(0,0))#当前位置坐标
    hash_location = db.ComputedProperty(lambda self:geohash.encode(self.location.lat, self.location.lon, 12))#hash过的当前位置坐标
    update_time = db.DateTimeProperty(auto_now_add=True)
